<?php
/*
 * @Author: 李明
 * @Date: 2021-11-30 18:32:14
 * @LastEditTime: 2021-12-06 11:12:27
 * @LastEditors: 李明
 * @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/app/data/LineChartController.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Controllers\app\data;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Utils;
use App\Models\Branch;
use App\Models\Worker;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon as SupportCarbon;

class LineChartController extends Controller
{
    /**
     * @description: 规模 折线图
     * @param {Request} $request
     * @return {*}
     */
    public function lineDataSizeList(Request $request)
    {
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
            }
            $all = $request->all();
            $worker_id = $tokenInfo['worker_id'];
            $worker = Worker::find($worker_id);
            $is_first = $all['is_first'] ?? 0; //   1是新单 2存量 3转续  传0是全部
            $time_type = $all['time_type'] ?? 1; //   1 周（近12周）  2 月（近12月）
            $branch_id = $all['branch_id'] ?? ($worker->branchesManage->first()->id ?? '');
            if ($branch_id == '') { // 则没有权限
                return LMReturn::Error('没有管理该部门的权限', 'CCC003');
            } else {
                $branch_self = Branch::find($branch_id);
                $branch_list_id = Utils::getChildSonBranchs($branch_self->id);
            }
        } catch (\Throwable $th) {
            return LMReturn::Error('传参出现问题，请联系管理员核查', 'CCC002');
        }
        try {
            $resultData = [];
            $sizeList = [];
            $standList = [];
            switch ($is_first) {
                case 1:
                    $sizeList[] = '新单';
                    $standList[] = '新单';
                    break;
                case 2:
                    $sizeList[] = '存量';
                    $standList[] = '存量';
                    break;
                case 3:
                    $sizeList[] = '转续';
                    $standList[] = '转续';
                    break;
                default:
                    $sizeList[] = '全部';
                    $standList[] = '全部';
                    break;
            }

            for ($i = 0; $i < 12; $i++) {
                if ($time_type == '1') { //1 周（近12周）  2 月（近12月）
                    $start_time = Carbon::now()->subWeeks($i)->startOfWeek(); // 获取这周开始时间
                    $end_time = Carbon::now()->subWeeks($i)->endOfWeek();
                } else if ($time_type == '2') {
                    $start_time = Carbon::now()->subMonths($i)->startOfMonth(); // 获取这月开始时间
                    $end_time = Carbon::now()->subMonths($i)->endOfMonth(); // 获取这月结束时间
                }
                $branchResultData = PieChartController::branchResult($branch_list_id, $start_time, $end_time, $is_first);
                $standList[] = $branchResultData['stand'] ?? 0;
                $sizeList[] = $branchResultData['size'] ?? 0;
            }
            $resultData[] = $sizeList;
            $resultData[] = $standList;
            return $resultData;
        } catch (\Throwable $th) {
            return LMReturn::Error('查询出现问题，请联系管理员核查', 'CCC001');
        }
    }
    /**
     * @description: 入金、出金、净入金、实际入金、手续费 折线图
     * @param {Request} $request
     * @return {*}
     */
    public function lineDataMoneyList(Request $request)
    {
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
            }
            $all = $request->all();
            $worker_id = $tokenInfo['worker_id'];
            $worker = Worker::find($worker_id);
            $time_type = $all['time_type'] ?? 1; //   1 周（近12周）  2 月（近12月）
            $branch_id = $all['branch_id'] ?? ($worker->branchesManage->first()->id ?? '');
            if ($branch_id == '') { // 则没有权限
                return LMReturn::Error('没有管理该部门的权限', 'CCC004');
            } else {
                $branch_self = Branch::find($branch_id);
                $branch_list_id = Utils::getChildSonBranchs($branch_self->id);
            }
        } catch (\Throwable $th) {
            return LMReturn::Error('传参出现问题，请联系管理员核查', 'CCC005');
        }
        try {
            //2、循环查询，当前月份往前12个月业绩
            $resultData = [];
            $golden_list[] = '入金';
            $withdraw_list[] = '出金';
            $pure_golden_list[] = '净入金';
            $true_golden_list[] = '实际入金';
            $course_list[] = '手续费';
            $cost_list[] = '运营费';
            for ($i = 0; $i < 12; $i++) {
                if ($time_type == '1') { //1 周（近12周）  2 月（近12月）
                    $start_time = Carbon::now()->subWeeks($i)->startOfWeek(); // 获取这周开始时间
                    $end_time = Carbon::now()->subWeeks($i)->endOfWeek();
                } else if ($time_type == '2') {
                    $start_time = Carbon::now()->subMonths($i)->startOfMonth(); // 获取这月开始时间
                    $end_time = Carbon::now()->subMonths($i)->endOfMonth(); // 获取这月结束时间
                }
                $branchResultData = PieChartController::branchPureGolden($branch_list_id, $start_time, $end_time);
                $golden_list[] = $branchResultData['golden'] ?? 0;
                $withdraw_list[] = $branchResultData['withdraw'] ?? 0;
                $pure_golden_list[] = $branchResultData['pure_golden'] ?? 0;
                $true_golden_list[] = $branchResultData['true_golden'] ?? 0;
                $course_list[] = $branchResultData['course'] ?? 0;
                $cost_list[] = $branchResultData['cost'] ?? 0;
            }
            $resultData[] = $golden_list;
            $resultData[] = $withdraw_list;
            $resultData[] = $pure_golden_list;
            $resultData[] = $true_golden_list;
            $resultData[] = $course_list;
            $resultData[] = $cost_list;
            return $resultData;
        } catch (\Throwable $th) {
            return LMReturn::Error('查询出现问题，请联系管理员核查', 'CCC006');
        }
    }
    /**
     * @description: 还本、付息 折线图
     * @param {Request} $request
     * @return {*}
     */
    public function lineDataCostList(Request $request)
    {
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
            }
            $all = $request->all();
            $worker_id = $tokenInfo['worker_id'];
            $worker = Worker::find($worker_id);
            $is_first = $all['is_first'] ?? 0; //   1是新单 2存量 3转续  传0是全部
            $time_type = $all['time_type'] ?? 1; //   1 周（近12周）  2 月（近12月）
            $branch_id = $all['branch_id'] ?? ($worker->branchesManage->first()->id ?? '');
            if ($branch_id == '') { // 则没有权限
                return LMReturn::Error('没有管理该部门的权限', 'CCC003');
            } else {
                $branch_self = Branch::find($branch_id);
                $branch_list_id = Utils::getChildSonBranchs($branch_self->id);
            }
        } catch (\Throwable $th) {
            return LMReturn::Error('传参出现问题，请联系管理员核查', 'CCC002');
        }
        try {
            //2、循环查询，当前月份往前12个月业绩
            $resultData = [];
            $moneyList = [];
            $interestList = [];
            $moneyList[] = '还本';
            $interestList[] = '付息';
            for ($i = 0; $i < 12; $i++) {
                if ($time_type == '1') { //1 周（近12周）  2 月（近12月）
                    $start_time = Carbon::now()->subWeeks($i)->startOfWeek(); // 获取这周开始时间
                    $end_time = Carbon::now()->subWeeks($i)->endOfWeek();
                } else if ($time_type == '2') {
                    $start_time = Carbon::now()->subMonths($i)->startOfMonth(); // 获取这月开始时间
                    $end_time = Carbon::now()->subMonths($i)->endOfMonth(); // 获取这月结束时间
                }
                $branchResultData = PieChartController::branchPrincipal($branch_list_id, $start_time, $end_time);
                $moneyList[] = $branchResultData['money'] ?? 0;
                $interestList[] = $branchResultData['interest'] ?? 0;
            }
            $resultData[] = $moneyList;
            $resultData[] = $interestList;
            return $resultData;
        } catch (\Throwable $th) {
            return LMReturn::Error('查询出现问题，请联系管理员核查', 'CCC001');
        }
    }
}
